package com.wgts.wgts.mvp.view.activity.homeclasschildactivity;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.control.statusBar.IsplayHelper;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.tools.AppInfor;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/1916:20
 *
 */public class ShopClassChildRecycleAdapter extends BaseLoadMoreAdapter<ShopClassChildRecycleAdapter.ViewHold> {
    private Context context;
    private List<SearchShopResultEntity.ShopList> list;
    private OnItemClickListener onItemClickListener;

    public ShopClassChildRecycleAdapter(Context context, List<SearchShopResultEntity.ShopList> list) {
        super(context);
        this.context = context;
        this.list = list;
    }

    @Override
    public void onBindBodyViewHolder(@NonNull final ViewHold viewHolder, int i) {
        if (onItemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = viewHolder.getLayoutPosition();
                    onItemClickListener.onItemClick(v, position);
                }
            });
        }
        MoHuGlide.getInstance().topRaduice(context, list.get(i).productSmallImage, viewHolder.imgProduct);

        CustomImageView.getInstance().customImage(context, viewHolder.imgProduct, 350/(double)750);

        viewHolder.tvProduce.setText(list.get(i).productName);
        viewHolder.tvPrice.setText("￥" + list.get(i).retailPrice);

        viewHolder.tvProduceSolve.setText("已售" + list.get(i).sellCount);



        if(AppInfor.getInstance().getUserState() > 1){
            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.black));

            viewHolder.placeholderText.setVisibility(View.VISIBLE);
            //viewHolder.tvOldPrice.getPaint().setFlags(null);
            viewHolder.tvOldPrice.setText(list.get(i).marketPrice);
            viewHolder.tvOldPrice.setTextSize(16);
            TextPaint tp = viewHolder.tvOldPrice.getPaint();
            tp.setFakeBoldText(true);
            viewHolder.tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));
            viewHolder.tvOldPrice.setPaintFlags(viewHolder.tvOldPrice.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        }else{

            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.red_bg));

            viewHolder.tvOldPrice.setText("￥" + list.get(i).marketPrice);
            viewHolder.placeholderText.setVisibility(View.GONE);
            viewHolder.tvOldPrice.setTextColor(ContextCompat.getColor(context, R.color.gray_text));
            viewHolder.tvOldPrice.setTextSize(11);
            TextPaint tp = viewHolder.tvOldPrice.getPaint();
            tp.setFakeBoldText(false);
            viewHolder.tvOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);



        }


    }

    @NonNull
    @Override
    public ViewHold onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate(R.layout.home_page_time_action, viewGroup, false));
    }

    @Override
    public int getBodyItemCount() {
        return list.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        return 0;
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.img_product)
        ImageView imgProduct;
        @BindView(R.id.tv_produce)
        TextView tvProduce;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_old_price)
        TextView tvOldPrice;
        @BindView(R.id.tv_state)
        TextView tvState;
        @BindView(R.id.linear_buy)
        LinearLayout linearBuy;
        @BindView(R.id.tv_produce_solve)
        TextView tvProduceSolve;
        @BindView(R.id.parent_ly)
        LinearLayout parentLy;
        @BindView(R.id.img_time_out)
        ImageView imgTimeOut;
        @BindView(R.id.placeholder_text)
        TextView placeholderText;
        private View itemView;
        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 设置某个View的margin
     *
     * @param view   需要设置的view
     * @param isDp   需要设置的数值是否为DP
     * @param left   左边距
     * @param right  右边距
     * @param top    上边距
     * @param bottom 下边距
     * @return
     */
    public static ViewGroup.LayoutParams setViewMargin(View view, boolean isDp, int left, int right, int top, int bottom) {
        if (view == null) {
            return null;
        }

        int leftPx = left;
        int rightPx = right;
        int topPx = top;
        int bottomPx = bottom;
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewGroup.MarginLayoutParams marginParams = null;
        //获取view的margin设置参数
        if (params instanceof ViewGroup.MarginLayoutParams) {
            marginParams = (ViewGroup.MarginLayoutParams) params;
        } else {
            //不存在时创建一个新的参数
            marginParams = new ViewGroup.MarginLayoutParams(params);
        }

        //根据DP与PX转换计算值
        if (isDp) {
            leftPx = IsplayHelper.dpToPx(left);
            rightPx = IsplayHelper.dpToPx(right);
            topPx = IsplayHelper.dpToPx(top);
            bottomPx = IsplayHelper.dpToPx(bottom);
        }
        //设置margin
        marginParams.setMargins(leftPx, topPx, rightPx, bottomPx);
        view.setLayoutParams(marginParams);
        return marginParams;
    }

}
